from flask_wtf import FlaskForm
from wtforms import Form, StringField, TextAreaField, PasswordField, SubmitField, BooleanField
from wtforms.validators import DataRequired, Length, Email, EqualTo, ValidationError

from wtforms import TextField, BooleanField, PasswordField, TextAreaField, validators
# # Article Form Class
# class ArticleForm(Form):
#     title = StringField(
#         '标题',
#         validators=[
#             # DataRequired(message= '标题长度应该在2-30字符之间'),
#             DataRequired(message='长度不少于5个字符'),
#             Length(min=2,max=30)
#         ]
#     )
#     content = TextAreaField(
#         '内容',
#         validators=[
#             DataRequired(message='长度不少于5个字符'),
#             Length(min=5)
#         ]
#     )
#
# # Register Form Class
# class RegisterForm(Form):
#     username = StringField(
#         '用户名',
#         validators=[
#             DataRequired(message='请输入用户名'),
#             Length(min=2, max=25,message='长度在2-25个字符之间')
#         ]
#     )
#     email = StringField(
#         '邮箱',
#         validators = [
#             DataRequired(message="请输入邮箱"),
#             Email(message='请输入正确的邮箱格式')
#         ]
#     )
#     password = PasswordField(
#         '密码',
#         validators = [
#             DataRequired(message='密码不能为空'),
#             Length(min=6,max=20,message='长度在6-20个字符之间'),
#         ]
#     )
#     confirm = PasswordField(
#         '确认密码',
#         validators=[
#             DataRequired(message='密码不能为空'),
#             Length(min=6, max=20),
#             EqualTo('password', message='2次输入密码不一致')
#         ]
#     )
#
# # 重置密码的按钮
# class ResetPasswordRequestForm(Form):
#     email =StringField('Email',validators=[DataRequired(),Email()])
#     sumbit =SubmitField('Request Password Reset')
from app.models import User


class LoginForm(FlaskForm):
    username = StringField('Username',validators=[DataRequired()])
    password = PasswordField('Password',validators=[DataRequired()])
    remember_me = BooleanField('Remember Me')

    submit = SubmitField('Sign In')
class RegistrationForm(FlaskForm):
    username =StringField('Username',validators=[DataRequired()])
    email =StringField('Email',validators=[DataRequired(),Email()])
    password =PasswordField('Password',validators=[DataRequired()])
    password2 = PasswordField(
        'Repeat Password',validators=[DataRequired(),EqualTo('password')])
    submit = SubmitField('Register')

    def validate_username(self,username):
        user =User.query.filter_by(username=username.data).first()
        if user is not None:
            raise ValidationError('Please use a different username.')

    def validate_email(self,email):
        user =User.query.filter_by(email=email.data).first()
        if user is not None:
            raise ValidationError('Please use a different email address.')

class PostForm(FlaskForm):
    post =TextAreaField('Say something',validators=[DataRequired(),Length(min=1,max=140)])
    submit = SubmitField('Submit')


class ResetPasswordRequestForm(FlaskForm):
    email =StringField('Email',validators=[DataRequired(),Email()])
    submit =SubmitField('Request Password Reset')
class ResetPasswordForm(FlaskForm):
    password = PasswordField('Password',validators=[DataRequired()])
    password2= PasswordField('Repeat Password',validators=[DataRequired(),EqualTo('password')])
    submit = SubmitField('Request Password Reset')


class EditProfileForm(FlaskForm):
    username = StringField('Username', validators=[DataRequired()])
    about_me = TextAreaField('About_me', validators=[Length(min=0, max=140)])
    submit = SubmitField('Submit')


class ArticleForm(Form):
    title = StringField(
        '标题',
        validators=[
            # DataRequired(message= '标题长度应该在2-30字符之间'),
            DataRequired(message='长度不少于5个字符'),
            Length(min=2,max=30)
        ]
    )
    content = TextAreaField(
        '内容',
        validators=[
            DataRequired(message='长度不少于5个字符'),
            Length(min=5)
        ]
    )
